﻿Imports System.Web.Mvc

Public Class WikiController
    Inherits Controller

    ' GET: /Wiki

    Private Function IsHomePageName(pageName As String)
        If pageName Is Nothing Then Return True
        pageName = pageName.ToLower()
        Return pageName = "" OrElse pageName = "index" OrElse pageName = "home"
    End Function

    Function FetchExact(page As String) As ActionResult
        If IsHomePageName(page) Then
            Return View("WikiIndex")
        End If
        ViewBag.Title = page
        If WikiModel.Instance.HasExactPage(page) Then
            Dim pageModel As WikiPageModel = WikiModel.Instance.GetExactPage(page)
            Return View(pageModel.GetViewName(), pageModel)
        Else
            Return View("WikiNoPage", CObj(page))
        End If
    End Function

    Function FetchNamed(page As String) As ActionResult
        If IsHomePageName(page) Then
            Return View("WikiIndex")
        End If
        ViewBag.Title = page
        If WikiModel.Instance.HasPage(page) Then
            Dim pageModel As WikiPageModel = WikiModel.Instance.GetPage(page)
            Dim v As ViewResult = View(pageModel.GetViewName(), pageModel)
            v.ViewData("SuggestSearch") = page
            Return v
        Else
            Return View("WikiNoPage", CObj(page))
        End If
    End Function

    Function FetchOrSearch(page As String) As ActionResult
        If IsHomePageName(page) Then
            Return View("WikiIndex")
        End If
        ViewBag.Title = page
        If WikiModel.Instance.HasPage(page) Then
            Dim pageModel As WikiPageModel = WikiModel.Instance.GetPage(page)
            Dim v As ViewResult = View(pageModel.GetViewName(), pageModel)
            v.ViewData("SuggestSearch") = page
            Return v
        Else
            Return RedirectToAction("Search", New With {.page = page})
        End If
    End Function

    Function FetchVersion(page As String, version As Long)
        ViewBag.Title = page & " - גרסת ארכיון"
        Try
            Return View("WikiPage", WikiModel.Instance.GetPageVersion(page, version))
        Catch e As WikiNoPageException
            Return View("WikiNoPage", CObj(page))
        End Try
    End Function

    <ValidateInput(False)>
    Function Edit(page As String, target As String, UpdatedPage As FiledWikiPageModel, Optional version As Long = 0) As ActionResult
        ViewBag.Title = page & " - עריכה"

        If UpdatedPage.Title Is Nothing Then
            If version > 0 Then
                Try
                    UpdatedPage = WikiModel.Instance.GetPageVersion(page, version)
                    UpdatedPage.LastChangeNotes = "שחזור גרסה מתאריך " & UpdatedPage.DateModeified.ToString
                Catch e As WikiNoPageVersionException
                    ' Return View("WikiNoPage", CObj(page)) Return no such version instead of no such page
                End Try
            ElseIf WikiModel.Instance.HasExactPage(page) Then
                Try
                    UpdatedPage = WikiModel.Instance.GetExactPage(page)
                    UpdatedPage.LastChangeNotes = ""
                Catch e As WikiNoPageException
                    Return View("WikiNoPage", CObj(page))
                End Try
            Else
                UpdatedPage = New FiledWikiPageModel(page)
                UpdatedPage.LastChangeNotes = "דף חדש"
            End If
        End If

        If target = "save" Then
            WikiModel.Instance.SavePage(UpdatedPage)
            Return RedirectToAction("FetchExact", New With {.page = page})

        ElseIf target = "remove" Then
            WikiModel.Instance.RemovePage(UpdatedPage)

        ElseIf target = "delete" Then
            WikiModel.Instance.DeletePage(UpdatedPage)
            Return RedirectToAction("FetchExact", New With {.page = ""})

        Else
            Dim v = View("WikiEditPage", UpdatedPage)

            If target = "preview" Then
                UpdatedPage.Content = MarkupTools.ProcessExtraMarkupHtml(UpdatedPage.Content)
                v.ViewData("preview") = True
            Else
                v.ViewData("preview") = False
            End If

            Return v
        End If
    End Function

    Function DoRename(page As String, NewName As String, Optional KeepOldName As Boolean = True) As ActionResult
        Dim pagemodel = WikiModel.Instance.GetExactPage(page)
        Wiki.WikiModel.Instance.RenamePage(pagemodel, NewName, KeepOldName)
        Return RedirectToAction("FetchExact", New With {.page = NewName})
    End Function

    Function Search(page As String) As ActionResult
        ViewBag.Title = page & " - חיפוש"
        Return View("WikiSearch", Wiki.WikiModel.Instance.Search(page))
    End Function

    Function Delete(page As String) As ActionResult
        ViewBag.Title = page & " - מחיקה"
        Try
            Return View("WikiDelete", WikiModel.Instance.GetPage(page))
        Catch e As WikiNoPageException
            Return View("WikiNoPage", CObj(page))
        End Try
    End Function

    Function Rename(page As String) As ActionResult
        ViewBag.Title = page & " - שינוי שם"
        Try
            Return View("WikiRename", WikiModel.Instance.GetPage(page))
        Catch e As WikiNoPageException
            Return View("WikiNoPage", CObj(page))
        End Try
    End Function

    Function History(page As String) As ActionResult
        ViewBag.Title = page & " - היסוטרית גרסאות"
        Try
            Return View("WikiHistory", WikiModel.Instance.GetPage(page))
        Catch e As WikiNoPageVersionException
            ' Return View("WikiNoPage", CObj(page)) Return no such version instead of no such page
        End Try
    End Function

    Function DoCompareVersions(page As String, versions As Dictionary(Of String, Boolean))
        Dim selectedVersions As New List(Of Long)
        For Each pair In versions
            If pair.Value Then
                selectedVersions.Add(CLng(pair.Key))
            End If
        Next
        If selectedVersions.Count <> 2 Then    Throw New Exception()
        selectedVersions.Sort()
        Return RedirectToAction("CompareVersions", New With {.page = page, .version1 = selectedVersions(0), .version2 = selectedVersions(1)})
    End Function
    Function CompareVersions(page As String, version1 As Long, version2 As Long)
        ViewBag.Title = page & " - השוואת גרסאות"
        Dim page1 = WikiModel.Instance.GetPageVersion(page, version1)
        Dim page2 = WikiModel.Instance.GetPageVersion(page, version2)
        Return View("WikiCompareVersions", New WikiPageCompersionModel(page1, page2))
    End Function


    Function SpecialPage(page As String)
        Return View("Special\" & page)
    End Function
    Function RandomPage()
        Dim titles = WikiModel.Instance.GetAllPageTitles()
        Dim rnd As New Random()
        Return RedirectToAction("FetchExact", New With {.page = titles(rnd.Next(titles.Count - 1))})
    End Function
End Class